package com.example.uchain.controller;

import cn.hutool.json.JSONObject;
import com.example.uchain.demo.PageBean;
import com.example.uchain.demo.Result;
import com.example.uchain.demo.WorksInformation;
import com.example.uchain.demo.dto.WorksInformationCountDTO;
import com.example.uchain.mapper.WorksInformationMapper;
import com.example.uchain.service.WorksInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@Slf4j//日志用的注解
@Api(tags = "作品信息")
public class WorksInformationController {

    @Autowired
    private WorksInformationService worksInformationService;
    @Autowired
    private WorksInformationMapper worksInformationMapper;


    @GetMapping("/information")
    @ApiOperation("查看所有作品信息")
    public Result findAll() {
        log.info("查看所有作品信息");
        List<WorksInformation> worksInformationList = worksInformationService.findAll();
        return Result.success(worksInformationList);
    }

    @PostMapping("/information")
    @ApiOperation("新增作品信息")
    public Result AddInformation(@RequestBody JSONObject param){
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        WorksInformation w = new WorksInformation();
        w.setWid(Long.valueOf((Integer) param.get("wid")));
        w.setMemberId(Long.valueOf((Integer) param.get("memberId")));
        w.setCreateAddress((String) param.get("createAddress"));
        w.setIntroduce((String) param.get("introduce"));
        w.setWorksName((String)param.get("worksName"));
        w.setWorksProperty((String) param.get("worksProperty"));
        w.setPublishAddress((String) param.get("publishAddress"));
        w.setCreateTime(LocalDateTime.parse((String)param.get("createTime"), formatter) );
        w.setPublishState((String) param.get("publishState"));
        w.setFirstpublishTime(LocalDateTime.parse((String)param.get("firstpublishTime"), formatter) );
        log.info("新增作品信息: {}",w);
        if (worksInformationMapper.findAllById(w.getWid())!=null)
        {
            return Result.QUERY_EXISTS;
        }
        worksInformationService.AddInformation(w);

        return Result.success();
    }

    @GetMapping("/information1")
    @ApiOperation("根据id查看作品信息")
    public Result page(@RequestParam(defaultValue = "1") Integer Page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String worksName,
                       String worksProperty ,
                       String publishAddress,
                       String publishState,
                       String createAddress,
                       Long wid,
                       Integer code){

        log.info("分页查询 参数:{},{},{},{},{},{},{},{},{}",Page,pageSize,worksName,worksProperty,publishAddress,publishState,createAddress,wid,code);
        PageBean pageBean=worksInformationService.page(Page,pageSize,worksName,worksProperty,publishAddress,publishState,createAddress,wid,code);
        return Result.success(pageBean);
    }

    @DeleteMapping ("/information")
    @ApiOperation("根据id删除作品信息")
    public Result deleteInformationById(@RequestParam("wid") Long  wid){
        log.info("根据id删除作品信息: {}",wid);
        return worksInformationService.deleteInformationById(wid);
    }

    @PutMapping( "/information")
    @ApiOperation("修改作品信息")
    public Result updateById(@RequestBody WorksInformation worksInformation){
        log.info("修改作品信息: {}", worksInformation);
        worksInformationService.updateById(worksInformation);
        return Result.success();
    }

    @GetMapping("/WorksInformationCount")
    @ApiOperation("总订单数量和七日内新增订单数量")
    public Result WorksInformationCount(){
        WorksInformationCountDTO counts = worksInformationService.WorksInformationCount();
        return Result.success(counts);
    }


}
